/**
 * 
 */
package com.zznode.client.wlanpolling.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;

/**
 * @author Pink
 * 
 */
public class PollTestAdaptor extends BaseExpandableListAdapter {
	public static final int TEST_AUTO_ALL = 0;
	public static final int TEST_MANUAL_PING = 1;
	public static final int TEST_MANUAL_COVERAGE = 2;
	public static final int TEST_MANUAL_APSWITCH = 3;
	public static final int TEST_MANUAL_APREL = 4;
	public static final int TEST_MANUAL_WEBAUTH = 5;
	public static final int TEST_MANUAL_FTPUPLOAD = 6;
	public static final int TEST_MANUAL_FTPDOWNLOAD = 7;

	private LayoutInflater mInflater = null;
	private List<GroupItem> groups;
	private Context context;

	public PollTestAdaptor(Activity ctx) {
		mInflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		context = ctx;
		initData();
	}

	public void initData() {
		groups = new ArrayList<GroupItem>();

		GroupItem groupItem = new GroupItem();
		groupItem.setGroupName("自动巡检测试项目");
		groupItem.addChildItem(new ChildItem(
				"IP自动获取参数设置\n数据吞吐率测试项目\n固定参数自动Ping测试", TEST_AUTO_ALL));
		groups.add(groupItem);

		groupItem = new GroupItem();
		groupItem.setGroupName("手动巡检测试项目");
		groupItem.addChildItem(new ChildItem("手工设置参数Ping测试", TEST_MANUAL_PING));
		groupItem.addChildItem(new ChildItem("场点覆盖电平及信号质量测试",
				TEST_MANUAL_COVERAGE));
		groupItem.addChildItem(new ChildItem("AP间切换测试", TEST_MANUAL_APSWITCH));
		//groupItem.addChildItem(new ChildItem("AP关联测试", TEST_MANUAL_APREL)); 界面封掉
		//TODO
		//groupItem.addChildItem(new ChildItem("Web认证测试", TEST_MANUAL_WEBAUTH));
		groups.add(groupItem);

		groupItem = new GroupItem();
		groupItem.setGroupName("FTP例行巡检项目测试");
		groupItem.addChildItem(new ChildItem("Ftp上传测试", TEST_MANUAL_FTPUPLOAD));
		groupItem
				.addChildItem(new ChildItem("Ftp下载测试", TEST_MANUAL_FTPDOWNLOAD));
		groups.add(groupItem);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return groups.get(groupPosition).getChildrenItem().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		View view = convertView;

		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.pollingtest_childitem, null);
		}

		TextView title = (TextView) view.findViewById(R.id.title);
		ChildItem childItem = groups.get(groupPosition).getChildrenItem()
				.get(childPosition);
		title.setText(childItem.getItemName());

		return view;

	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return groups.get(groupPosition).getChildrenItem().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = mInflater.inflate(R.layout.pollingtest_group, null);
		}
		TextView groupView = (TextView) view.findViewById(R.id.title);
		GroupItem groupItem = groups.get(groupPosition);
		groupView.setText(groupItem.getGroupName());

		ImageView groupIndicator = (ImageView) view
				.findViewById(R.id.group_indicator);
		if (!isExpanded) {
			groupIndicator.setImageResource(R.drawable.icon_close);
		} else {
			groupIndicator.setImageResource(R.drawable.icon_open);
		}

		return view;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}

class GroupItem {
	private String groupName;
	private List<ChildItem> childrenItem;

	public GroupItem() {
		childrenItem = new ArrayList<ChildItem>();
		groupName = "";
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public List<ChildItem> getChildrenItem() {
		return childrenItem;
	}

	public void setChildrenItem(List<ChildItem> childrenItem) {
		this.childrenItem = childrenItem;
	}

	public void addChildItem(ChildItem item) {
		childrenItem.add(item);
	}
}

class ChildItem {
	private String itemName;
	private int tag;

	public ChildItem(String itemName, int tag) {
		this.itemName = itemName;
		this.tag = tag;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public int getTag() {
		return tag;
	}

}
